const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const foot = require('../../templates/foot/index.js');
var cityData = require('../../utils/area.js');
var loginStatus = wx.getStorageSync("loginStatus");
const {
  $Toast
} = require('../../components/base/base');
var tabBar = foot.tabBar;
var tabBarList = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: false,
    navBox: false,
    tabBar: [],
  },

  // 获取商品券
  GetTicketList: function () {
    var that = this;
    wx.request({
      url: util.api + api.GetTicketList,
      data: {
        BUSINESS_PARAMETERS: {
          userToekn: loginStatus.token
        },
        SYSTEM_PARAMETERS: {
          APP_ID: "M_P_G001",
          METHOD_ID: 0,
          ACCESS_TOKEN: "1w2Re3bE^og38",
          TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
          SIGN: "string"
        }
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.MESSAGE.MESSAGE_CODE == 1) {
          that.setData({
            account: res.data.RESULT
          })
          console.log(res.data.RESULT)
        }

      },
      fail: function (res) {

      }
    })
  },

  // 商品券减少
  minTicket: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    var account = that.data.account;
    if (account[parseInt(index)].counts > 1) {
      account[parseInt(index)].counts--;
      that.setData({
        account: account
      });

    }
  },

  // 商品券增加
  addTicket: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var account = that.data.account;
    if (index !== "" && index != null) {
      account[parseInt(index)].counts++;
      that.setData({
        account: account
      });
    }

  },

  // 监听输入
  ticketInput: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var account = that.data.account;
    account[parseInt(index)].counts = e.detail.value.replace(/[^0-9]/g, '')

  },

  // 购买
  buyTicket: function (e) {
    var that = this;
    var account = that.data.account;
    for (var i in account) {
      if (account[i].id == e.currentTarget.dataset.id) {
        if (account[i].creditlimit <= 500) {
          that.modal.showModal();
          that.setData({
            ticketContent: {
              ticketValArea: account[i].market,
              ticketValNumber: account[i].counts,
              ticketValPrice: account[i].price
            }
          })
        } else {
          that.dialog.showDialog();
        }
      }

    }

  },
  //页面加载时购物车数量
  countCart: function (e) {
    var that = this;
    wx.request({
      url: util.api + api.GetShopcart,
      data: {
        BUSINESS_PARAMETERS: {
          userToekn: loginStatus.token,
          PostStaId: 22
        },
        SYSTEM_PARAMETERS: {
          APP_ID: "M_P_G001",
          METHOD_ID: 0,
          ACCESS_TOKEN: "1w2Re3bE^og38",
          TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
          SIGN: "string"
        }
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        if (res.data.MESSAGE.MESSAGE_CODE == 1) {
          var arr = res.data.RESULT || [];
          var counts = 0;
          //遍历购物车对其进行求和
          for (var i = 0; i < arr.length; i++) {
            counts = parseInt(counts) + parseInt(arr[i].Num);
          }
          let navCont = that.data.navCont;
          console.log(arr)
          //购物车商品数量
          that.setData({
            navCont: counts
          })
        }

      },
      fail: function (res) {}
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.GetTicketList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    this.countCart();
    for (let i = 0; i < tabBar.length; i++) {
      tabBar[0].style = 0;
      tabBar[1].style = 0;
      tabBar[2].style = 1;
      tabBar[3].style = 0;
      tabBar[4].style = 0;
      tabBar[3].ico = 1;
      tabBarList.push(tabBar[i])
    }
    that.setData({
      tabBar: tabBarList
    })
    tabBarList.length = 0;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})